home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / devel / lang / lisp / gcl-1.000 / gcl-1 / gcl-1.0 / c / unexelf.c < prev    next >
Encoding:
Text File  |  1994-04-14  |  28.6 KB  |  792 lines

  1. /* Copyright (C) 1985, 1986, 1987, 1988, 1990, 1992
  2.    Free Software Foundation, Inc.
  3.  
  4.     This program is free software; you can redistribute it and/or modify
  5.     it under the terms of the GNU Library General Public License as published by
  6.     the Free Software Foundation; either version 2, or (at your option)
  7.     any later version.
  8.  
  9.     This program is distributed in the hope that it will be useful,
  10.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.     GNU Library General Public License for more details.
  13.  
  14.     You should have received a copy of the GNU Library General Public License
  15.     along with this program; if not, write to the Free Software
  16.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17.  
  18. In other words, you are welcome to use, share and improve this program.
  19. You are forbidden to forbid anyone else to use, share and improve
  20. what you give them.   Help stamp out software-hoarding!  */
  21.  
  22. /*
  23.  * unexec.c - Convert a running program into an a.out file.
  24.  *
  25.  * Author:    Spencer W. Thomas
  26.  *         Computer Science Dept.
  27.  *         University of Utah
  28.  * Date:    Tue Mar  2 1982
  29.  * Modified heavily since then.
  30.  *
  31.  * Synopsis:
  32.  *    unexec (new_name, a_name, data_start, bss_start, entry_address)
  33.  *    char *new_name, *a_name;
  34.  *    unsigned data_start, bss_start, entry_address;
  35.  *
  36.  * Takes a snapshot of the program and makes an a.out format file in the
  37.  * file named by the string argument new_name.
  38.  * If a_name is non-NULL, the symbol table will be taken from the given file.
  39.  * On some machines, an existing a_name file is required.
  40.  *
  41.  * The boundaries within the a.out file may be adjusted with the data_start
  42.  * and bss_start arguments.  Either or both may be given as 0 for defaults.
  43.  *
  44.  * Data_start gives the boundary between the text segment and the data
  45.  * segment of the program.  The text segment can contain shared, read-only
  46.  * program code and literal data, while the data segment is always unshared
  47.  * and unprotected.  Data_start gives the lowest unprotected address.
  48.  * The value you specify may be rounded down to a suitable boundary
  49.  * as required by the machine you are using.
  50.  *
  51.  * Specifying zero for data_start means the boundary between text and data
  52.  * should not be the same as when the program was loaded.
  53.  * If NO_REMAP is defined, the argument data_start is ignored and the
  54.  * segment boundaries are never changed.
  55.  *
  56.  * Bss_start indicates how much of the data segment is to be saved in the
  57.  * a.out file and restored when the program is executed.  It gives the lowest
  58.  * unsaved address, and is rounded up to a page boundary.  The default when 0
  59.  * is given assumes that the entire data segment is to be stored, including
  60.  * the previous data and bss as well as any additional storage allocated with
  61.  * break (2).
  62.  *
  63.  * The new file is set up to start at entry_address.
  64.  *
  65.  * If you make improvements I'd like to get them too.
  66.  * harpo!utah-cs!thomas, thomas@Utah-20
  67.  *
  68.  */
  69.  
  70. /* Even more heavily modified by james@bigtex.cactus.org of Dell Computer Co.
  71.  * ELF support added.
  72.  *
  73.  * Basic theory: the data space of the running process needs to be
  74.  * dumped to the output file.  Normally we would just enlarge the size
  75.  * of .data, scooting everything down.  But we can't do that in ELF,
  76.  * because there is often something between the .data space and the
  77.  * .bss space.
  78.  *
  79.  * In the temacs dump below, notice that the Global Offset Table
  80.  * (.got) and the Dynamic link data (.dynamic) come between .data1 and
  81.  * .bss.  It does not work to overlap .data with these fields.
  82.  *
  83.  * The solution is to create a new .data segment.  This segment is
  84.  * filled with data from the current process.  Since the contents of
  85.  * various sections refer to sections by index, the new .data segment
  86.  * is made the last in the table to avoid changing any existing index.
  87.  
  88.  * This is an example of how the section headers are changed.  "Addr"
  89.  * is a process virtual address.  "Offset" is a file offset.
  90.  
  91. raid:/nfs/raid/src/dist-18.56/src> dump -h temacs
  92.  
  93. temacs:
  94.  
  95.            **** SECTION HEADER TABLE ****
  96. [No]    Type    Flags   Addr         Offset       Size          Name
  97.         Link    Info    Adralgn      Entsize
  98.  
  99. [1]     1       2       0x80480d4    0xd4         0x13          .interp
  100.         0       0       0x1          0            
  101.  
  102. [2]     5       2       0x80480e8    0xe8         0x388         .hash
  103.         3       0       0x4          0x4          
  104.  
  105. [3]     11      2       0x8048470    0x470        0x7f0         .dynsym
  106.         4       1       0x4          0x10         
  107.  
  108. [4]     3       2       0x8048c60    0xc60        0x3ad         .dynstr
  109.         0       0       0x1          0            
  110.  
  111. [5]     9       2       0x8049010    0x1010       0x338         .rel.plt
  112.         3       7       0x4          0x8          
  113.  
  114. [6]     1       6       0x8049348    0x1348       0x3           .init
  115.         0       0       0x4          0            
  116.  
  117. [7]     1       6       0x804934c    0x134c       0x680         .plt
  118.         0       0       0x4          0x4          
  119.  
  120. [8]     1       6       0x80499cc    0x19cc       0x3c56f       .text
  121.         0       0       0x4          0            
  122.  
  123. [9]     1       6       0x8085f3c    0x3df3c      0x3           .fini
  124.         0       0       0x4          0            
  125.  
  126. [10]    1       2       0x8085f40    0x3df40      0x69c         .rodata
  127.         0       0       0x4          0            
  128.  
  129. [11]    1       2       0x80865dc    0x3e5dc      0xd51         .rodata1
  130.         0       0       0x4          0            
  131.  
  132. [12]    1       3       0x8088330    0x3f330      0x20afc       .data
  133.         0       0       0x4          0            
  134.  
  135. [13]    1       3       0x80a8e2c    0x5fe2c      0x89d         .data1
  136.         0       0       0x4          0            
  137.  
  138. [14]    1       3       0x80a96cc    0x606cc      0x1a8         .got
  139.         0       0       0x4          0x4          
  140.  
  141. [15]    6       3       0x80a9874    0x60874      0x80          .dynamic
  142.         4       0       0x4          0x8          
  143.  
  144. [16]    8       3       0x80a98f4    0x608f4      0x449c        .bss
  145.         0       0       0x4          0            
  146.  
  147. [17]    2       0       0            0x608f4      0x9b90        .symtab
  148.         18      371     0x4          0x10         
  149.  
  150. [18]    3       0       0            0x6a484      0x8526        .strtab
  151.         0       0       0x1          0            
  152.  
  153. [19]    3       0       0            0x729aa      0x93          .shstrtab
  154.         0       0       0x1          0            
  155.  
  156. [20]    1       0       0            0x72a3d      0x68b7        .comment
  157.         0       0       0x1          0            
  158.  
  159. raid:/nfs/raid/src/dist-18.56/src> dump -h xemacs
  160.  
  161. xemacs:
  162.  
  163.            **** SECTION HEADER TABLE ****
  164. [No]    Type    Flags   Addr         Offset       Size          Name
  165.         Link    Info    Adralgn      Entsize
  166.  
  167. [1]     1       2       0x80480d4    0xd4         0x13          .interp
  168.         0       0       0x1          0            
  169.  
  170. [2]     5       2       0x80480e8    0xe8         0x388         .hash
  171.         3       0       0x4          0x4          
  172.  
  173. [3]     11      2       0x8048470    0x470        0x7f0         .dynsym
  174.         4       1       0x4          0x10         
  175.  
  176. [4]     3       2       0x8048c60    0xc60        0x3ad         .dynstr
  177.         0       0       0x1          0            
  178.  
  179. [5]     9       2       0x8049010    0x1010       0x338         .rel.plt
  180.         3       7       0x4          0x8          
  181.  
  182. [6]     1       6       0x8049348    0x1348       0x3           .init
  183.         0       0       0x4          0            
  184.  
  185. [7]     1       6       0x804934c    0x134c       0x680         .plt
  186.         0       0       0x4          0x4          
  187.  
  188. [8]     1       6       0x80499cc    0x19cc       0x3c56f       .text
  189.         0       0       0x4          0            
  190.  
  191. [9]     1       6       0x8085f3c    0x3df3c      0x3           .fini
  192.         0       0       0x4          0            
  193.  
  194. [10]    1       2       0x8085f40    0x3df40      0x69c         .rodata
  195.         0       0       0x4          0            
  196.  
  197. [11]    1       2       0x80865dc    0x3e5dc      0xd51         .rodata1
  198.         0       0       0x4          0            
  199.  
  200. [12]    1       3       0x8088330    0x3f330      0x20afc       .data
  201.         0       0       0x4          0            
  202.  
  203. [13]    1       3       0x80a8e2c    0x5fe2c      0x89d         .data1
  204.         0       0       0x4          0            
  205.  
  206. [14]    1       3       0x80a96cc    0x606cc      0x1a8         .got
  207.         0       0       0x4          0x4          
  208.  
  209. [15]    6       3       0x80a9874    0x60874      0x80          .dynamic
  210.         4       0       0x4          0x8          
  211.  
  212. [16]    8       3       0x80c6800    0x7d800      0             .bss
  213.         0       0       0x4          0            
  214.  
  215. [17]    2       0       0            0x7d800      0x9b90        .symtab
  216.         18      371     0x4          0x10         
  217.  
  218. [18]    3       0       0            0x87390      0x8526        .strtab
  219.         0       0       0x1          0            
  220.  
  221. [19]    3       0       0            0x8f8b6      0x93          .shstrtab
  222.         0       0       0x1          0            
  223.  
  224. [20]    1       0       0            0x8f949      0x68b7        .comment
  225.         0       0       0x1          0            
  226.  
  227. [21]    1       3       0x80a98f4    0x608f4      0x1cf0c       .data
  228.         0       0       0x4          0            
  229.  
  230.  * This is an example of how the file header is changed.  "Shoff" is
  231.  * the section header offset within the file.  Since that table is
  232.  * after the new .data section, it is moved.  "Shnum" is the number of
  233.  * sections, which we increment.
  234.  *
  235.  * "Phoff" is the file offset to the program header.  "Phentsize" and
  236.  * "Shentsz" are the program and section header entries sizes respectively.
  237.  * These can be larger than the apparent struct sizes.
  238.  
  239. raid:/nfs/raid/src/dist-18.56/src> dump -f temacs
  240.  
  241. temacs:
  242.  
  243.                     **** ELF HEADER ****
  244. Class        Data       Type         Machine     Version
  245. Entry        Phoff      Shoff        Flags       Ehsize
  246. Phentsize    Phnum      Shentsz      Shnum       Shstrndx
  247.  
  248. 1            1          2            3           1
  249. 0x80499cc    0x34       0x792f4      0           0x34
  250. 0x20         5          0x28         21          19
  251.  
  252. raid:/nfs/raid/src/dist-18.56/src> dump -f xemacs
  253.  
  254. xemacs:
  255.  
  256.                     **** ELF HEADER ****
  257. Class        Data       Type         Machine     Version
  258. Entry        Phoff      Shoff        Flags       Ehsize
  259. Phentsize    Phnum      Shentsz      Shnum       Shstrndx
  260.  
  261. 1            1          2            3           1
  262. 0x80499cc    0x34       0x96200      0           0x34
  263. 0x20         5          0x28         22          19
  264.  
  265.  * These are the program headers.  "Offset" is the file offset to the
  266.  * segment.  "Vaddr" is the memory load address.  "Filesz" is the
  267.  * segment size as it appears in the file, and "Memsz" is the size in
  268.  * memory.  Below, the third segment is the code and the fourth is the
  269.  * data: the difference between Filesz and Memsz is .bss
  270.  
  271. raid:/nfs/raid/src/dist-18.56/src> dump -o temacs
  272.  
  273. temacs:
  274.  ***** PROGRAM EXECUTION HEADER *****
  275. Type        Offset      Vaddr       Paddr
  276. Filesz      Memsz       Flags       Align
  277.  
  278. 6           0x34        0x8048034   0           
  279. 0xa0        0xa0        5           0           
  280.  
  281. 3           0xd4        0           0           
  282. 0x13        0           4           0           
  283.  
  284. 1           0x34        0x8048034   0           
  285. 0x3f2f9     0x3f2f9     5           0x1000      
  286.  
  287. 1           0x3f330     0x8088330   0           
  288. 0x215c4     0x25a60     7           0x1000      
  289.  
  290. 2           0x60874     0x80a9874   0           
  291. 0x80        0           7           0           
  292.  
  293. raid:/nfs/raid/src/dist-18.56/src> dump -o xemacs
  294.  
  295. xemacs:
  296.  ***** PROGRAM EXECUTION HEADER *****
  297. Type        Offset      Vaddr       Paddr
  298. Filesz      Memsz       Flags       Align
  299.  
  300. 6           0x34        0x8048034   0           
  301. 0xa0        0xa0        5           0           
  302.  
  303. 3           0xd4        0           0           
  304. 0x13        0           4           0           
  305.  
  306. 1           0x34        0x8048034   0           
  307. 0x3f2f9     0x3f2f9     5           0x1000      
  308.  
  309. 1           0x3f330     0x8088330   0           
  310. 0x3e4d0     0x3e4d0     7           0x1000      
  311.  
  312. 2           0x60874     0x80a9874   0           
  313. 0x80        0           7           0           
  314.  
  315.  
  316.  */
  317.  
  318. /* Modified by wtien@urbana.mcd.mot.com of Motorola Inc. 
  319.  * 
  320.  * The above mechanism does not work if the unexeced ELF file is being
  321.  * re-layout by other applications (such as `strip'). All the applications 
  322.  * that re-layout the internal of ELF will layout all sections in ascending
  323.  * order of their file offsets. After the re-layout, the data2 section will 
  324.  * still be the LAST section in the section header vector, but its file offset 
  325.  * is now being pushed far away down, and causes part of it not to be mapped
  326.  * in (ie. not covered by the load segment entry in PHDR vector), therefore 
  327.  * causes the new binary to fail.
  328.  *
  329.  * The solution is to modify the unexec algorithm to insert the new data2
  330.  * section header right before the new bss section header, so their file
  331.  * offsets will be in the ascending order. Since some of the section's (all 
  332.  * sections AFTER the bss section) indexes are now changed, we also need to 
  333.  * modify some fields to make them point to the right sections. This is done 
  334.  * by macro PATCH_INDEX. All the fields that need to be patched are:
  335.  * 
  336.  * 1. ELF header e_shstrndx field.
  337.  * 2. section header sh_link and sh_info field.
  338.  * 3. symbol table entry st_shndx field.
  339.  *
  340.  * The above example now should look like:
  341.  
  342.            **** SECTION HEADER TABLE ****
  343. [No]    Type    Flags   Addr         Offset       Size          Name
  344.         Link    Info    Adralgn      Entsize
  345.  
  346. [1]     1       2       0x80480d4    0xd4         0x13          .interp
  347.         0       0       0x1          0            
  348.  
  349. [2]     5       2       0x80480e8    0xe8         0x388         .hash
  350.         3       0       0x4          0x4          
  351.  
  352. [3]     11      2       0x8048470    0x470        0x7f0         .dynsym
  353.         4       1       0x4          0x10         
  354.  
  355. [4]     3       2       0x8048c60    0xc60        0x3ad         .dynstr
  356.         0       0       0x1          0            
  357.  
  358. [5]     9       2       0x8049010    0x1010       0x338         .rel.plt
  359.         3       7       0x4          0x8          
  360.  
  361. [6]     1       6       0x8049348    0x1348       0x3           .init
  362.         0       0       0x4          0            
  363.  
  364. [7]     1       6       0x804934c    0x134c       0x680         .plt
  365.         0       0       0x4          0x4          
  366.  
  367. [8]     1       6       0x80499cc    0x19cc       0x3c56f       .text
  368.         0       0       0x4          0            
  369.  
  370. [9]     1       6       0x8085f3c    0x3df3c      0x3           .fini
  371.         0       0       0x4          0            
  372.  
  373. [10]    1       2       0x8085f40    0x3df40      0x69c         .rodata
  374.         0       0       0x4          0            
  375.  
  376. [11]    1       2       0x80865dc    0x3e5dc      0xd51         .rodata1
  377.         0       0       0x4          0            
  378.  
  379. [12]    1       3       0x8088330    0x3f330      0x20afc       .data
  380.         0       0       0x4          0            
  381.  
  382. [13]    1       3       0x80a8e2c    0x5fe2c      0x89d         .data1
  383.         0       0       0x4          0            
  384.  
  385. [14]    1       3       0x80a96cc    0x606cc      0x1a8         .got
  386.         0       0       0x4          0x4          
  387.  
  388. [15]    6       3       0x80a9874    0x60874      0x80          .dynamic
  389.         4       0       0x4          0x8          
  390.  
  391. [16]    1       3       0x80a98f4    0x608f4      0x1cf0c       .data
  392.         0       0       0x4          0            
  393.  
  394. [17]    8       3       0x80c6800    0x7d800      0             .bss
  395.         0       0       0x4          0            
  396.  
  397. [18]    2       0       0            0x7d800      0x9b90        .symtab
  398.         19      371     0x4          0x10         
  399.  
  400. [19]    3       0       0            0x87390      0x8526        .strtab
  401.         0       0       0x1          0            
  402.  
  403. [20]    3       0       0            0x8f8b6      0x93          .shstrtab
  404.         0       0       0x1          0            
  405.  
  406. [21]    1       0       0            0x8f949      0x68b7        .comment
  407.         0       0       0x1          0            
  408.  
  409.  */
  410.  
  411. #include <sys/types.h>
  412. #include <stdio.h>
  413. #include <sys/stat.h>
  414. #include <memory.h>
  415. #include <string.h>
  416. #include <errno.h>
  417. #include <unistd.h>
  418. #include <fcntl.h>
  419. #include <elf.h>
  420. #include <sys/mman.h>
  421.  
  422. #ifndef emacs
  423. #define fatal(a, b, c) fprintf (stderr, a, b, c), exit (1)
  424. #else
  425. extern void fatal (char *, ...);
  426. #endif
  427.  
  428. /* Get the address of a particular section or program header entry,
  429.  * accounting for the size of the entries.
  430.  */
  431.  
  432. #define OLD_SECTION_H(n) \
  433.      (*(Elf32_Shdr *) ((byte *) old_section_h + old_file_h->e_shentsize * (n)))
  434. #define NEW_SECTION_H(n) \
  435.      (*(Elf32_Shdr *) ((byte *) new_section_h + new_file_h->e_shentsize * (n)))
  436. #define OLD_PROGRAM_H(n) \
  437.      (*(Elf32_Phdr *) ((byte *) old_program_h + old_file_h->e_phentsize * (n)))
  438. #define NEW_PROGRAM_H(n) \
  439.      (*(Elf32_Phdr *) ((byte *) new_program_h + new_file_h->e_phentsize * (n)))
  440.  
  441. #define PATCH_INDEX(n) \
  442.   do { \
  443.      if ((int) (n) >= old_bss_index) \
  444.        (n)++; } while (0)
  445. typedef unsigned char byte;
  446.  
  447. /* Round X up to a multiple of Y.  */
  448.  
  449. int
  450. round_up (x, y)
  451.      int x, y;
  452. {
  453.   int rem = x % y;
  454.   if (rem == 0)
  455.     return x;
  456.   return x - rem + y;
  457. }
  458.  
  459. /* ****************************************************************
  460.  * unexec
  461.  *
  462.  * driving logic.
  463.  *
  464.  * In ELF, this works by replacing the old .bss section with a new
  465.  * .data section, and inserting an empty .bss immediately afterwards.
  466.  *
  467.  */
  468. void
  469. unexec (new_name, old_name, data_start, bss_start, entry_address)
  470.      char *new_name, *old_name;
  471.      unsigned data_start, bss_start, entry_address;
  472. {
  473.   extern unsigned int bss_end;
  474.   int new_file, old_file, new_file_size;
  475.  
  476.   /* Pointers to the base of the image of the two files. */
  477.   caddr_t old_base, new_base;
  478.  
  479.   /* Pointers to the file, program and section headers for the old and new
  480.    * files.
  481.    */
  482.   Elf32_Ehdr *old_file_h, *new_file_h;
  483.   Elf32_Phdr *old_program_h, *new_program_h;
  484.   Elf32_Shdr *old_section_h, *new_section_h;
  485.  
  486.   /* Point to the section name table in the old file */
  487.   char *old_section_names;
  488.  
  489.   Elf32_Addr old_bss_addr, new_bss_addr;
  490.   Elf32_Word old_bss_size, new_data2_size;
  491.   Elf32_Off  new_data2_offset;
  492.   Elf32_Addr new_data2_addr;
  493.  
  494.   int n, nn, old_bss_index, old_data_index, new_data2_index;
  495.   struct stat stat_buf;
  496.  
  497.   /* Open the old file & map it into the address space. */
  498.  
  499.   old_file = open (old_name, O_RDONLY);
  500.  
  501.   if (old_file < 0)
  502.     fatal ("Can't open %s for reading: errno %d\n", old_name, errno);
  503.  
  504.   if (fstat (old_file, &stat_buf) == -1)
  505.     fatal ("Can't fstat (%s): errno %d\n", old_name, errno);
  506.  
  507.   old_base = mmap (0, stat_buf.st_size, PROT_READ, MAP_SHARED, old_file, 0);
  508.  
  509.   if (old_base == (caddr_t) -1)
  510.     fatal ("Can't mmap (%s): errno %d\n", old_name, errno);
  511.  
  512. #ifdef DEBUG
  513.   fprintf (stderr, "mmap (%s, %x) -> %x\n", old_name, stat_buf.st_size,
  514.        old_base);
  515. #endif
  516.  
  517.   /* Get pointers to headers & section names */
  518.  
  519.   old_file_h = (Elf32_Ehdr *) old_base;
  520.   old_program_h = (Elf32_Phdr *) ((byte *) old_base + old_file_h->e_phoff);
  521.   old_section_h = (Elf32_Shdr *) ((byte *) old_base + old_file_h->e_shoff);
  522.   old_section_names = (char *) old_base
  523.     + OLD_SECTION_H (old_file_h->e_shstrndx).sh_offset;
  524.  
  525.   /* Find the old .bss section.  Figure out parameters of the new
  526.    * data2 and bss sections.
  527.    */
  528.  
  529.   for (old_bss_index = 1; old_bss_index < (int) old_file_h->e_shnum;
  530.        old_bss_index++)
  531.     {
  532. #ifdef DEBUG
  533.       fprintf (stderr, "Looking for .bss - found %s\n",
  534.            old_section_names + OLD_SECTION_H (old_bss_index).sh_name);
  535. #endif
  536.       if (!strcmp (old_section_names + OLD_SECTION_H (old_bss_index).sh_name,
  537.            ".bss"))
  538.     break;
  539.     }
  540.   if (old_bss_index == old_file_h->e_shnum)
  541.     fatal ("Can't find .bss in %s.\n", old_name, 0);
  542.  
  543.   old_bss_addr = OLD_SECTION_H (old_bss_index).sh_addr;
  544.   old_bss_size = OLD_SECTION_H (old_bss_index).sh_size;
  545. #if defined(emacs) || !defined(DEBUG)
  546.   bss_end = (unsigned int) sbrk (0);
  547.   new_bss_addr = (Elf32_Addr) bss_end;
  548. #else
  549.   new_bss_addr = old_bss_addr + old_bss_size + 0x1234;
  550. #endif
  551.   new_data2_addr = old_bss_addr;
  552.   new_data2_size = new_bss_addr - old_bss_addr;
  553.   new_data2_offset = OLD_SECTION_H (old_bss_index).sh_offset;
  554.  
  555. #ifdef DEBUG
  556.   fprintf (stderr, "old_bss_index %d\n", old_bss_index);
  557.   fprintf (stderr, "old_bss_addr %x\n", old_bss_addr);
  558.   fprintf (stderr, "old_bss_size %x\n", old_bss_size);
  559.   fprintf (stderr, "new_bss_addr %x\n", new_bss_addr);
  560.   fprintf (stderr, "new_data2_addr %x\n", new_data2_addr);
  561.   fprintf (stderr, "new_data2_size %x\n", new_data2_size);
  562.   fprintf (stderr, "new_data2_offset %x\n", new_data2_offset);
  563. #endif
  564.  
  565.   if ((unsigned) new_bss_addr < (unsigned) old_bss_addr + old_bss_size)
  566.     fatal (".bss shrank when undumping???\n", 0, 0);
  567.  
  568.   /* Set the output file to the right size and mmap it.  Set
  569.    * pointers to various interesting objects.  stat_buf still has
  570.    * old_file data.
  571.    */
  572.  
  573.   new_file = open (new_name, O_RDWR | O_CREAT, 0666);
  574.   if (new_file < 0)
  575.     fatal ("Can't creat (%s): errno %d\n", new_name, errno);
  576.  
  577.   new_file_size = stat_buf.st_size + old_file_h->e_shentsize + new_data2_size;
  578.  
  579.   if (ftruncate (new_file, new_file_size))
  580.     fatal ("Can't ftruncate (%s): errno %d\n", new_name, errno);
  581.  
  582.   new_base = mmap (0, new_file_size, PROT_READ | PROT_WRITE, MAP_SHARED,
  583.            new_file, 0);
  584.  
  585.   if (new_base == (caddr_t) -1)
  586.     fatal ("Can't mmap (%s): errno %d\n", new_name, errno);
  587.  
  588.   new_file_h = (Elf32_Ehdr *) new_base;
  589.   new_program_h = (Elf32_Phdr *) ((byte *) new_base + old_file_h->e_phoff);
  590.   new_section_h = (Elf32_Shdr *)
  591.     ((byte *) new_base + old_file_h->e_shoff + new_data2_size);
  592.  
  593.   /* Make our new file, program and section headers as copies of the
  594.    * originals.
  595.    */
  596.  
  597.   memcpy (new_file_h, old_file_h, old_file_h->e_ehsize);
  598.   memcpy (new_program_h, old_program_h,
  599.       old_file_h->e_phnum * old_file_h->e_phentsize);
  600.  
  601.   /* Modify the e_shstrndx if necessary. */
  602.   PATCH_INDEX (new_file_h->e_shstrndx);
  603.  
  604.   /* Fix up file header.  We'll add one section.  Section header is
  605.    * further away now.
  606.    */
  607.  
  608.   new_file_h->e_shoff += new_data2_size;
  609.   new_file_h->e_shnum += 1;
  610.  
  611. #ifdef DEBUG
  612.   fprintf (stderr, "Old section offset %x\n", old_file_h->e_shoff);
  613.   fprintf (stderr, "Old section count %d\n", old_file_h->e_shnum);
  614.   fprintf (stderr, "New section offset %x\n", new_file_h->e_shoff);
  615.   fprintf (stderr, "New section count %d\n", new_file_h->e_shnum);
  616. #endif
  617.  
  618.   /* Fix up a new program header.  Extend the writable data segment so
  619.    * that the bss area is covered too. Find that segment by looking
  620.    * for a segment that ends just before the .bss area.  Make sure
  621.    * that no segments are above the new .data2.  Put a loop at the end
  622.    * to adjust the offset and address of any segment that is above
  623.    * data2, just in case we decide to allow this later.
  624.    */
  625.  
  626.   for (n = new_file_h->e_phnum - 1; n >= 0; n--)
  627.     {
  628.       /* Compute maximum of all requirements for alignment of section.  */
  629.       int alignment = (NEW_PROGRAM_H (n)).p_align;
  630.       if ((OLD_SECTION_H (old_bss_index)).sh_addralign > alignment)
  631.     alignment = OLD_SECTION_H (old_bss_index).sh_addralign;
  632.  
  633.       if (NEW_PROGRAM_H (n).p_vaddr + NEW_PROGRAM_H (n).p_filesz > old_bss_addr)
  634.     fatal ("Program segment above .bss in %s\n", old_name, 0);
  635.  
  636.       if (NEW_PROGRAM_H (n).p_type == PT_LOAD
  637.       && (round_up ((NEW_PROGRAM_H (n)).p_vaddr
  638.             + (NEW_PROGRAM_H (n)).p_filesz,
  639.             alignment)
  640.           == round_up (old_bss_addr, alignment)))
  641.     break;
  642.     }
  643.   if (n < 0)
  644.     fatal ("Couldn't find segment next to .bss in %s\n", old_name, 0);
  645.  
  646.   NEW_PROGRAM_H (n).p_filesz += new_data2_size;
  647.   NEW_PROGRAM_H (n).p_memsz = NEW_PROGRAM_H (n).p_filesz;
  648.  
  649. #if 0 /* Maybe allow section after data2 - does this ever happen? */
  650.   for (n = new_file_h->e_phnum - 1; n >= 0; n--)
  651.     {
  652.       if (NEW_PROGRAM_H (n).p_vaddr
  653.       && NEW_PROGRAM_H (n).p_vaddr >= new_data2_addr)
  654.     NEW_PROGRAM_H (n).p_vaddr += new_data2_size - old_bss_size;
  655.  
  656.       if (NEW_PROGRAM_H (n).p_offset >= new_data2_offset)
  657.     NEW_PROGRAM_H (n).p_offset += new_data2_size;
  658.     }
  659. #endif
  660.  
  661.   /* Fix up section headers based on new .data2 section.  Any section
  662.    * whose offset or virtual address is after the new .data2 section
  663.    * gets its value adjusted.  .bss size becomes zero and new address
  664.    * is set.  data2 section header gets added by copying the existing
  665.    * .data header and modifying the offset, address and size.
  666.    */
  667.   for (old_data_index = 1; old_data_index < (int) old_file_h->e_shnum;
  668.        old_data_index++)
  669.     if (!strcmp (old_section_names + OLD_SECTION_H (old_data_index).sh_name,
  670.          ".data"))
  671.       break;
  672.   if (old_data_index == old_file_h->e_shnum)
  673.     fatal ("Can't find .data in %s.\n", old_name, 0);
  674.  
  675.   /* Walk through all section headers, insert the new data2 section right 
  676.      before the new bss section. */
  677.   for (n = 1, nn = 1; n < (int) old_file_h->e_shnum; n++, nn++)
  678.     {
  679.       caddr_t src;
  680.       /* If it is bss section, insert the new data2 section before it. */
  681.       if (n == old_bss_index)
  682.     {
  683.       /* Steal the data section header for this data2 section. */
  684.       memcpy (&NEW_SECTION_H (nn), &OLD_SECTION_H (old_data_index),
  685.           new_file_h->e_shentsize);
  686.       
  687.       NEW_SECTION_H (nn).sh_addr = new_data2_addr;
  688.       NEW_SECTION_H (nn).sh_offset = new_data2_offset;
  689.       NEW_SECTION_H (nn).sh_size = new_data2_size;
  690.       /* Use the bss section's alignment. This will assure that the
  691.          new data2 section always be placed in the same spot as the old
  692.          bss section by any other application. */
  693.       NEW_SECTION_H (nn).sh_addralign = OLD_SECTION_H (n).sh_addralign;
  694.  
  695.       /* Now copy over what we have in the memory now. */
  696.       memcpy (NEW_SECTION_H (nn).sh_offset + new_base, 
  697.           (caddr_t) OLD_SECTION_H (n).sh_addr, 
  698.           new_data2_size);
  699.       nn++;
  700.     }
  701.       
  702.       memcpy (&NEW_SECTION_H (nn), &OLD_SECTION_H (n), 
  703.           old_file_h->e_shentsize);
  704.       
  705.       /* The new bss section's size is zero, and its file offset and virtual
  706.      address should be off by NEW_DATA2_SIZE. */
  707.       if (n == old_bss_index)
  708.     {
  709.       /* NN should be `old_bss_index + 1' at this point. */
  710.       NEW_SECTION_H (nn).sh_offset += new_data2_size;
  711.       NEW_SECTION_H (nn).sh_addr += new_data2_size;
  712.       /* Let the new bss section address alignment be the same as the
  713.          section address alignment followed the old bss section, so 
  714.          this section will be placed in exactly the same place. */
  715.       NEW_SECTION_H (nn).sh_addralign = OLD_SECTION_H (nn).sh_addralign;
  716.       NEW_SECTION_H (nn).sh_size = 0;
  717.     }
  718.       /* Any section that was original placed AFTER the bss section should now
  719.      be off by NEW_DATA2_SIZE. */
  720.       else if (NEW_SECTION_H (nn).sh_offset >= new_data2_offset)
  721.     NEW_SECTION_H (nn).sh_offset += new_data2_size;
  722.       
  723.       /* If any section hdr refers to the section after the new .data
  724.      section, make it refer to next one because we have inserted 
  725.      a new section in between. */
  726.       
  727.       PATCH_INDEX (NEW_SECTION_H (nn).sh_link);
  728.       PATCH_INDEX (NEW_SECTION_H (nn).sh_info);
  729.       
  730.       /* Now, start to copy the content of sections. */
  731.       if (NEW_SECTION_H (nn).sh_type == SHT_NULL
  732.       || NEW_SECTION_H (nn).sh_type == SHT_NOBITS)
  733.     continue;
  734.       
  735.       /* Write out the sections. .data and .data1 (and data2, called
  736.        * ".data" in the strings table) get copied from the current process
  737.        * instead of the old file.
  738.        */
  739.       if (!strcmp (old_section_names + NEW_SECTION_H (n).sh_name, ".data")
  740.       || !strcmp ((old_section_names + NEW_SECTION_H (n).sh_name),
  741.               ".data1"))
  742.     src = (caddr_t) OLD_SECTION_H (n).sh_addr;
  743.       else
  744.     src = old_base + OLD_SECTION_H (n).sh_offset;
  745.       
  746.       memcpy (NEW_SECTION_H (nn).sh_offset + new_base, src,
  747.           NEW_SECTION_H (nn).sh_size);
  748.  
  749.       /* If it is the symbol table, its st_shndx field needs to be patched. */
  750.       if (NEW_SECTION_H (nn).sh_type == SHT_SYMTAB
  751.       || NEW_SECTION_H (nn).sh_type == SHT_DYNSYM)
  752.     {
  753.       Elf32_Shdr *spt = &NEW_SECTION_H (nn);
  754.       unsigned int num = spt->sh_size / spt->sh_entsize;
  755.       Elf32_Sym * sym = (Elf32_Sym *) (NEW_SECTION_H (nn).sh_offset + 
  756.                        new_base);
  757.       for (; num--; sym++)
  758.         {
  759.           if ((sym->st_shndx == SHN_UNDEF)
  760.           || (sym->st_shndx == SHN_ABS)
  761.           || (sym->st_shndx == SHN_COMMON))
  762.         continue;
  763.     
  764.           PATCH_INDEX (sym->st_shndx);
  765.         }
  766.     }
  767.     }
  768.  
  769.   /* Close the files and make the new file executable */
  770.  
  771.   if (close (old_file))
  772.     fatal ("Can't close (%s): errno %d\n", old_name, errno);
  773.  
  774.   if (close (new_file))
  775.     fatal ("Can't close (%s): errno %d\n", new_name, errno);
  776.  
  777.   if (stat (new_name, &stat_buf) == -1)
  778.     fatal ("Can't stat (%s): errno %d\n", new_name, errno);
  779.  
  780.   n = umask (777);
  781.   umask (n);
  782.   stat_buf.st_mode |= 0111 & ~n;
  783.   if (chmod (new_name, stat_buf.st_mode) == -1)
  784.     fatal ("Can't chmod (%s): errno %d\n", new_name, errno);
  785. }
  786.  
  787.  
  788.  
  789. #ifdef UNIXSAVE
  790. #include "save.c"
  791. #endif
  792.